<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>宇成建筑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" href="../statics/css/font.css">
    <link rel="stylesheet" href="../statics/plugins/layui/css/layui.css">
    <link rel="stylesheet" href="../statics/css/xadmin.css">
    <link rel="stylesheet" href="../statics/css/style.css">
    <style>
        .layui-table td, .layui-table th {
            min-width: 60px;
        }
    </style>
</head>

<body class='labelAuto'>
<div class="x-nav">
        <span class="layui-breadcrumb">
            <a href="welcome.html">首页</a>
            <a href="javascript:;">财务</a>
            <a href="javascript:;">项目生活费</a>
            <a href="javascript:;">项目账簿</a>
            <a>
                <cite>账簿明细</cite></a>
        </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right;margin-right:10px"
       onclick="window.history.go(-1)" title="后退">
        <i class="iconfontBlod" style="line-height:30px">&#xe74f;</i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body search">
                    <form action="" class="layui-form livingList">
                        <div class="layui-form-item">
                        </div>
                    </form>
                </div>
                <div class="layui-card-body tableCardBody">
                    <table class="layui-table" id="lists" lay-filter="test"></table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script type='text/html' id='toolbar'>
    <button class="layui-btn add" type="button" auth="addLoan">
        添加
    </button>
</script>
<script src="../statics/plugins/layui/layui.js" charset="utf-8"></script>
<script src='../statics/plugins/jQuery-3.2.1/jquery-3.2.1.min.js'></script>
<script type="text/javascript" src="../statics/plugins/X-admin/xadmin.js"></script>
<script type="text/javascript" src="../statics/js/public.js?v=1129"></script>
<script type="text/javascript">
    layui.use(['form', 'layer', "table", "laydate"], function () {
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var layer = layui.layer;
        var bookId = getQueryString('bookId');
        var projectId = getQueryString('projectId');
        var title = '';
        var type = '';
        $.ajax({
            url: url + "/finance/living/bookInfo",
            type: "POST",
            async: false,
            data: {
                id: bookId,
                token: getToken()
            },
            success: function (r) {
                if (r.data.status === 3){
                    $('#toolbar').empty();
                }
                if (r.data.type == 1){
                    title = '充值';
                    type = 'recharge';
                } else{
                    title = '退款';
                    type = 'refund';
                }
            }
        });

        // 异步数据加载
        var lists = table.render({
            elem: '#lists', //绑定DOM元素
            id: 'lists', //table的id
            title: '人员列表',
            height: 'full-150',
            contentType: "application/json; charset=utf-8",
            url: url + '/finance/living/accountLists', //数据接口地址
            request: {
                pageName: 'page', //页码的参数名称，默认：page
                limitName: 'length' //每页数据量的参数名，默认：limit
            },
            method: 'POST',
            parseData: function (res) {
                return {
                    "code": res.code, //解析接口状态
                    "data": res.data.data,
                    "msg": res.msg,
                    "count": res.data.count
                };
            },
            where: {
                bookId: bookId,
                token: getToken()
            },
            toolbar: '#toolbar',
            defaultToolbar: ['filter'],
            page: true, //开启分页
            limit: 10, //默认每页记录条数
            limits: [10, 20, 50], //每页记录条数可选数量
            cols: [
                [ //表头
                    {field: '', title: '编号', type: 'numbers', width: '70',totalRowText:'合计：'},
                    {field: 'projectName', title: '项目名称', width: '180'},
                    {field: 'employeeName', title: '工人', width: '100'},
                    {field: 'account', title: title+'金额', width: '120','totalRowText':'读取中...',templet:function(d){
                            return numberFormat(Number(d.account),'currency');
                        }},
                    {field: 'remark', title: '备注', width: '300'},
                    {
                        field: 'livingTime', title: title+'时间', width: '90', templet: function (d) {
                            return (d.livingTime).substr(0, 10);
                        }
                    },
                    {field: 'adminName', title: '录入人', width: '70'},
                    {
                        field: 'status', title: '审批状态', width: '80', templet: function (d) {
                            switch(d.status){
                                case 2:
                                    return '审核中';
                                case 3:
                                    return '封存';
                                default:
                                    return '审核中';
                            }
                        }
                    },
                    { field: '', title: '操作',width:'100',templet(d){
                            if (d.status !== 3){
                                return `<button class='layui-btn layui-btn-danger' auth="deleteLoan" lay-event="delete" data-id=${d.id}>删除借款</button>`
                            }else{
                                return '';
                            }
                        }}
                ]
            ],
            response: {
                statusName: 'code', //数据状态的字段名称，默认：code
                statusCode: 100000, //成功的状态码，默认：0
                msgName: 'msg', //状态信息的字段名称，默认：msg
                countName: 'count', //数据总数的字段名称，默认：count
                dataName: 'data' //数据列表的字段名称，默认：data
            },
            totalRow:true,
            done: function () {
                totalRow();
                permission();
            }
        });
        table.on('tool(test)', function (data) {
            if (data.event === 'delete'){
                var id = data.data.id;
                layer.open({
                    type: 0,
                    title: "删除",
                    area: ['40%', '30%'],
                    content: `请确认是否删除此条`+title+`记录`,
                    btn: ['确认', '取消'],
                    skin: 'my-skin',
                    yes: function (index, layero) {
                        $.ajax({
                            url: url + "/finance/living/deleteAccount",
                            type: "POST",
                            async: false,
                            data: {
                                id: id,
                                token: getToken()
                            },
                            success: function (r) {
                                if (r.code == 100000) {
                                    tips('删除', r);
                                    setTimeout(function () {
                                        layer.closeAll();
                                        table.reload('lists');
                                    }, 2000)
                                } else {
                                    tips('删除', r);
                                }
                            }
                        })
                    }
                })
            }
        });
        //搜索
        $('body').on('click', '#search', function () {
            table.reload('lists', {
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    bookId: bookId,
                    token: getToken()
                }
            })
        });

        //添加记录
        $('body').on('click', 'button.add', function () {
            layer.open({
                type: 1,
                title: title,
                area: ['40%', 'auto'],
                content: `
                    <form class='layui-form layerOpen'>
                        <div class='layui-item'>
                            <div class='layui-inline'>
                                <label class='layui-form-label'>查询姓名</label>
                                <div class='layui-input-block'>
                                    <input type='text' placeholder='请输入姓名或工号' name='search' autocomplete="off" class='layui-input'>
                                    <ul class='layerOpen_ul_list'></ul>
                                </div>
                            </div>
                        </div>
                        <div class="layui-item">
                            <div class='layui-inline'>
                                <label class="layui-form-label">${title}金额</label>
                                <div class="layui-input-block">
                                    <input type="number" class="layui-input" name="account" min="0">
                                </div>
                            </div>
                        </div>
                        <div class="layui-item">
                            <div class='layui-inline'>
                                <label class="layui-form-label">${title}时间</label>
                                <div class="layui-input-block">
                                    <input type="text" class="layui-input" name="livingTime">
                                </div>
                            </div>
                        </div>
                        <div class="layui-item">
                            <div class='layui-inline'>
                                <label class="layui-form-label">备注</label>
                                <div class="layui-input-block">
                                    <input type="text" class="layui-input" name="remark">
                                </div>
                            </div>
                        </div>
                    </form>
                `,
                btn: ['确认', '取消'],
                skin: 'my-skin',
                success: function () {
                    $('select[name=projectId]').empty().html(projectSelect());
                    form.render();
                    laydate.render({
                        elem: 'input[name=livingTime]'
                        , type: 'date'
                        , value: new Date()
                        , max: 0
                    });
                },
                yes: function (index, layero) {
                    var employeeId = $('input[name=search]').data('id');
                    var account = $('input[name=account]').val();
                    var livingTime = $('input[name=livingTime]').val();
                    var remark = $('input[name=remark]').val();
                    $.ajax({
                        url: url + "/finance/living/addAccount",
                        type: "POST",
                        async: false,
                        data: {
                            projectId: projectId,
                            bookId: bookId,
                            employeeId: employeeId,
                            account: account,
                            livingTime: livingTime,
                            remark: remark,
                            token: getToken()
                        },
                        success: function (r) {
                            if (r.code == 100000) {
                                tips(title, r);
                                setTimeout(function () {
                                    layer.closeAll();
                                    table.reload('lists');
                                }, 2000)
                            } else {
                                tips(title, r);
                            }
                        }
                    })
                }
            })
        })

        $('body').on('input propertychange', 'input[name=search]', function () {
            var val = $(this).val();
            var this_ind = $(this);
            var this_next = $(this).next();
            $.ajax({
                type: "post",
                url: url + '/hrm/employee/search',
                beforeSend: function () {
                    $('#dayTime').attr('data-id', '');
                },
                data: {
                    projectId: Number(getParam('projectId')),
                    search: val,
                    token: getParam('token')
                },
                success: function (r) {
                    this_next.show();
                    if (r.data == "" || r.data == null) {
                        this_next.html('未查找到此人');
                    } else {
                        var li = '';
                        for (var i = 0; i < r.data.length; i++) {
                            li += `<li data-id=${r.data[i].id}>${r.data[i].name}</li>`
                        }
                        this_next.html(li);
                        $('ul li').click(function () {
                            this_ind.val($(this).html());
                            this_ind.attr('data-id', $(this).data('id'));
                            $('ul').hide();
                        })
                    }
                }
            })
        })

        function totalRow(){
            $.ajax({
                url: url + '/finance/living/livingCollect',
                type: 'post',
                data: {
                    bookId: bookId,
                    token: getToken()
                },
                dataType: 'json',
                success: function (r) {
                    if (r.code == 100000) {
                        $('.layui-table-total td[data-field=account] div').empty().html(r.data[t]?numberFormat(r.data[t],'currency'):'-')
                    }
                }
            });
        }
    });

</script>

</html>